package com.pand.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.pand.mysql.model.Order;
import com.pand.netty.NettyServer;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created by lh on 2017/11/8.
 */
public class OrderPushThread implements Runnable {
    public static final BlockingDeque<Order> ORDERS = new LinkedBlockingDeque<>();

    @Override
    public void run() {
        while (true) {
            try {
                Order order = ORDERS.take();
//                NettyServer.sendMsg(JSON.toJSONString(order, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue));
                handleOrder(order);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void handleOrder(Order order) {
        switch (order.getStatus()) {
            case "1010":
                order.setStatus(Order.StatusType.RESERVED.getCode());
                order.setStatusName(Order.StatusType.RESERVED.getValue());
                sendMsg(order);
                break;
            case "1030":
                order.setStatus(Order.StatusType.USED.getCode());
                order.setStatusName(Order.StatusType.USED.getValue());
                sendMsg(order);
                break;
            case "1050":
                order.setStatus(Order.StatusType.RETURNED.getCode());
                order.setStatusName(Order.StatusType.RETURNED.getValue());
                sendMsg(order);
                break;
            case "1070":
                order.setStatus(Order.StatusType.COMPLETED.getCode());
                order.setStatusName(Order.StatusType.COMPLETED.getValue());
                sendMsg(order);
                break;
            case "1091":
                order.setStatus(Order.StatusType.CANCELLED.getCode());
                order.setStatusName(Order.StatusType.CANCELLED.getValue());
                sendMsg(order);
                break;
            case "1092":
                order.setStatus(Order.StatusType.CLOSED.getCode());
                order.setStatusName(Order.StatusType.CLOSED.getValue());
                sendMsg(order);
                break;
            default:
                break;
        }
    }

    private void sendMsg(Order order) {
        NettyServer.sendMsg(JSON.toJSONString(order, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat,SerializerFeature.WriteNullStringAsEmpty));
    }

}
